/**主控制器*/
Ext.define("core.app.controller.MainController2", {
    extend: "Ext.app.Controller",
    init: function () {
        var self = this;
        this.addFunItem = function (funInfo) {
            if (funInfo) {
                var mainView = funInfo.mainView;
                var funPanel = mainView.down(funInfo.funViewXtype);
                if (!funPanel) {
                    self.application.getController(funInfo.funController).init();
                    funPanel = Ext.create(funInfo.funViewName);
                    mainView.add(funPanel);
                    mainView.setActiveTab(funPanel);
                } else {
                    mainView.setActiveTab(funPanel);
                }
            }
        },
            ///**下在是控制部分*/
            this.control({
                'westview': {
                    afterrender: this.getRootId
                }
            }, {
                /**注销*/
                "topview button[ref=logout]": {
                    click: function (btn) {
                        Ext.Ajax.request({
                            waitMsg: '正在注销……',
                            url: baseUrl + "/user/logout.do",
                            method: "POST",
                            timeout: 4000,
                            success: function (response, opts) {
                                var resObj = Ext.decode(response.responseText);
                                if (resObj.success) {
                                    function callBack() {
                                        location.href = baseUrl + "/oms/login.jsp";
                                    }

                                    Ext.Msg.alert("提示", resObj.msg, callBack);
                                } else {
                                    Ext.Msg.alert("提示", resObj.msg);
                                }
                            }
                        });
                    }
                }//"westview treepanel" end
            });
    },
    getRootId: function () {
        Ext.getBody().mask('正在加载系统菜单,请稍后...');
        Ext.Ajax.request({
            url: baseUrl + '/sys/loadRootMenuId.do',
            method: 'POST',
            // 回调
            callback: function (options, success, response) {
                treeMenuEven(Ext.JSON
                    .decode(response.responseText));
            }
        });
    },
    views: [
        "core.app.view.TopView",
        "core.app.view.WestView",
        "core.app.view.CenterView",
        "core.app.view.MainViewLayout"//,
    ],
    store: [],
    model: ['treeModel']
});

function treeMenuEven(data) {
    for (var i = 0; i < data.length; i++) {
        Ext.getCmp('treePanel').add(Ext.create("Ext.tree.Panel", {
            title: data[i].text,
            iconCls: data[i].iconCls,
            useArrows: true,
            autoScroll: true,
            rootVisible: false,
            store: treeStore(data[i].id),
            listeners: {
                'select': function (me, record, item, index, e) {
                    Ext.Ajax.request({
                        url: baseUrl + '/sys/getUrlByMenuId.do',
                        params: {
                            menuId: record.data.id
                        },
                        method: 'POST',
                        callback: function (options, success, response) {
                            var centerPanel = Ext.getCmp("centerid");
                            var mainView = centerPanel;
                            var funPanel = mainView.down("userlayout");
                            if (!funPanel) {
                                Ext.require(
                                    "core.user.controller.UserController",     // this auto-loads all dependencies
                                    function () {
                                        // ... as soon as this class
                                        //    and all its dependencies have loaded...
                                        Ext.create("core.user.controller.UserController").init();
                                        funPanel = Ext.create("core.user.view.UserLayout");
                                        mainView.add(funPanel);
                                        mainView.setActiveTab(funPanel);// launch init() method
                                    }
                                );

                            } else {
                                mainView.setActiveTab(funPanel);
                            }
                            // var tab = centerPanel.getComponent("tab" + record.data.id); //获取tab对象
                            // if (!tab) {//如果tab不存在，就创建并添加到centerPanel中
                            //     tab = Ext.create("userlayout");
                            //     centerPanel.add(tab);
                            // }
                            // centerPanel.setActiveTab(tab); //设置显示当前面板
                            Ext.Msg.alert("提示", Ext.JSON.decode(response.responseText).url);
                        }
                    });
                }
            }
        }));
    }
    Ext.getBody().unmask();
}

var treeStore = function (id) {
    return Ext.create("Ext.data.TreeStore", {
        defaultRootId: id, // 默认的根节点id
        model: 'treeModel',
        proxy: {
            type: "ajax", // 获取方式
            url: baseUrl + "/sys/loadNodeMenuId.do" // 获取树节点的地址
        },
        nodeParam: "pid"// 设置传递给后台的参数名,值是树节点的id属性
    });
};